package com.itheima.controller;

import com.itheima.common.Result;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理Controller
 */
@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
@Autowired
    private DeptService deptService;


@Autowired
private EmpService empService;

    /**
     * 查询全部部门信息
     * @return
     */
    @GetMapping
    public Result<List<Dept>> list(){
        List<Dept> depts = deptService.list();
        return Result.success(depts);
    }

    /**
     * 按照id删除学生
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @Transactional(rollbackFor = Exception.class)
public Result deleteById(@PathVariable Integer id){
        deptService.removeById(id);
        //empService.removeByDeptId(id);
        return  Result.success();
}

    /**
     * 添加单个学生
     * @param dept
     * @return
     */
    @PostMapping
public Result insertOne(@RequestBody Dept dept){
    deptService.insertById(dept);

    return Result.success();
}

    /**
     * 查询单个学生
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectOne(@PathVariable String id){
        Dept dept = deptService.selectOne(id);

        return Result.success(dept);
    }

    /**
     * 添加单个学生
     * @param dept
     * @return
     */
    @PutMapping
    public Result putOne(@RequestBody Dept dept){
        deptService.updateOne(dept);

        return Result.success();


    }



}
